package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.BaseUserInfo;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//注册
public class UserZCActivity extends BaseActivity {
    @BindView(R.id.cd_title_btn)
    TextView cdTitleBtn;
    @BindView(R.id.cd_title_bu)
    TextView cdTitleBu;
    @BindView(R.id.edit_yzm)
    EditText editYzm;
    @BindView(R.id.tv_input_tip)
    TextView tvInputTip;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.edit_pwdok)
    EditText editPwdok;
    @BindView(R.id.btn_res)
    Button btnRes;
    @BindView(R.id.btn_look_a)
    ImageView btnLookA;
    @BindView(R.id.btn_look_b)
    ImageView btnLookB;
    private boolean pwd_a, pwd_b = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        editInit();
    }

    private void editInit() {
        btnLookA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pwd_a) {
                    pwd_a = false;
                    btnLookA.setImageResource(R.mipmap.denglulookopen);
                    editPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    pwd_a = true;
                    btnLookA.setImageResource(R.mipmap.denglulookno);
                    editPwd.setInputType(InputType.TYPE_CLASS_TEXT);
                }
            }
        });
        btnLookB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pwd_b) {
                    pwd_b = false;
                    btnLookB.setImageResource(R.mipmap.denglulookopen);
                    editPwdok.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    pwd_b = true;
                    btnLookB.setImageResource(R.mipmap.denglulookno);
                    editPwdok.setInputType(InputType.TYPE_CLASS_TEXT);
                }
            }
        });
    }


    public void volleyGet() {
        String url = Constants.ZHUCE;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("注册", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        Tools.toast(msg);
                        startAction(UserLogInActivity.class);
                    } else {
                        Tools.toast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userPhone", BaseUserInfo.phone);
                    json_url.put("userPassword", editPwd.getText().toString());
                    json_url.put("messageCode", editYzm.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }


    @OnClick(R.id.btn_res)
    public void onClick() {
        volleyGet();
    }
}
